package com.zoo.boot.controller;

import com.zoo.boot.api.CommonResult;
import com.zoo.boot.dto.MenuDto;
import com.zoo.boot.dto.MenuIndexDto;
import com.zoo.boot.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: WangB
 * @Date: 2021/6/29
 */
@Controller
@Api(tags = "SysMenuController", value = "菜单管理")
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/build")
    @ResponseBody
    @ApiOperation("构建 dtree")
    public CommonResult buildMenuAll(){
        return CommonResult.tableSuccess(menuService.buildMenuAll());
    }



    @GetMapping("/ebuild/{roleId}")
    @ResponseBody
    @ApiOperation("绘制 复选菜单树")
    public CommonResult buildMenuAllByRoleId(@PathVariable Integer roleId){
        List<MenuDto> menuAll =menuService.buildMenuAllByRoleId(roleId);
        return CommonResult.tableSuccess(menuAll);
    }


    @GetMapping("/index")
    @ApiOperation("展示菜单界面")
    public String index(){
        return "system/menu/menu";
    }


    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("获取菜单列表")
    public CommonResult list(){
        return  CommonResult.tableSuccess(menuService.selectMenuList());
    }

    @GetMapping("/add")
    @ApiOperation("展示添加页面")
    public String navAddPage(){
        return "system/menu/menu-add";
    }


    @PostMapping
    @ResponseBody
    @ApiOperation("保存菜单数据")
    public CommonResult addMenu(@RequestBody MenuIndexDto info) {
        if(menuService.insertMenu(info)){
            return  CommonResult.success();
        }
        return CommonResult.fail();
    }

    @GetMapping("/update")
    @ApiOperation("展示修改页面")
    public String navUpdatePage(MenuIndexDto info,Model model){
        MenuIndexDto menuIndexDto = menuService.selectMenuById(info.getId());
        model.addAttribute("menuInfo",menuIndexDto);
        return  "system/menu/menu-edit";
    }


    @PutMapping
    @ResponseBody
    @ApiOperation("修改菜单")
    public CommonResult  update(@RequestBody MenuIndexDto info){
        if(menuService.update(info)){
            return  CommonResult.success();
        }
        return CommonResult.fail();
    }


    @DeleteMapping
    @ResponseBody
    @ApiOperation("删除菜单")
    public CommonResult  delete(Integer id){
        if(menuService.delete(id)){
            return  CommonResult.success();
        }
        return  CommonResult.fail();
    }
}
